package com.loongtech.bi.manager.realTime;

import com.loongtech.bi.action.PageHelper;
import com.loongtech.bi.manager.realTime.OverviewManager.OverviewData;
import com.loongtech.bi.manager.realTime.OverviewManager.OverviewDetailData;
import com.loongtech.bi.manager.system.ConfigManager;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.Time;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Author: yuri
 * Description: 国家实时
 * Date: 10:05 2017/6/16
 */
@Service
public class OverviewCountryManager extends ManagerQueryCacheBase<Object> {
    private static final long serialVersionUID = 1L;

    @Resource
    private NewUserByDayManager newUserByDayManager;
    @Resource
    private DistinctNewUserByDayManager distinctNewUserByDayManager;
    @Resource
    private NewUserByHourByTagManager newUserByHourByTagManager;
    @Resource
    private DistinctNewUserByHourManager distinctNewUserByHourManager;
    @Resource
    private ActiveDeviceByDayManager activeDeviceByDayManager;
    @Resource
    private NewDeviceByDayManager newDeviceByDayManager;
    @Resource
    private DauByDayManager dauByDayManager;
    @Resource
    private DauByHourByTagManager dauByHourByTagManager;

    @Resource
    private AddCashUserByHourByTagManager addCashUserByHourByTagManager;

    @Resource
    private AddCashUserByDayManager addCashUserByDayManager;

    @Resource
    private CashAddByDayManager cashAddByDayManager;
    @Resource
    private CashAddByHourByTagManager cashAddByHourByTagManager;
    @Resource
    private NewUserAddCashUserByHourByTagManager newUserAddCashUserByHourByTagManager;
    @Resource
    private NewUserCashAddByHourByTagManager newUserCashAddByHourByTagManager;

    @Resource
    private UserSecondLeftByDayManager userSecondLeftByDayManager;

    @Resource
    private UserThirdLeftByDayManager userThirdLeftByDayManager;

    @Resource
    private NewUserActiveByHourManager user2LeftByHourManager;

    @Resource
    private ConfigManager configManager;

    public String getMoneyType() {
        return configManager.getConfigValueByName(ConfigManager.MONEY_TYPE) == null ? "($)" : configManager.getConfigValueByName(ConfigManager.MONEY_TYPE);
    }

    // 得到今日实时汇总数据
    public List<OverviewData> getOverviewData(Date lookDate, String queryPlatform, String queryChannel, String queryServer, String queryCountry) {
        List<OverviewData> result = new ArrayList<>();
        // 激活设备数
        OverviewManager overviewManager = new OverviewManager();
//      OverviewManager.OverviewData o0 = overviewManager.new OverviewData();
//        o0.setTitle("激活设备数");
//        // 新增设备数
//        OverviewData o01 = new OverviewData();
//        o01.setTitle("新增设备数");
//        // 激活新增转换率
//        OverviewData o02 = new OverviewData();
//        o02.setTitle("激活新增转换率");
        // 新增账号数
        OverviewManager.OverviewData o1 = overviewManager.new OverviewData();
        o1.setTitle("新增账号数");
        // 去重新增账号数
//        OverviewData o100 = new OverviewData();
//        o100.setTitle("去重新增账号数");
        // 活跃账号数
        OverviewManager.OverviewData o2 = overviewManager.new OverviewData();
        o2.setTitle("活跃账号数");
        // 付费账号数
        OverviewManager.OverviewData o3 = overviewManager.new OverviewData();
        o3.setTitle("付费账号数");
        // 付费金额=
        OverviewManager.OverviewData o4 = overviewManager.new OverviewData();
        o4.setTitle("付费金额" + getMoneyType());
        // 活跃付费渗透率
        OverviewManager.OverviewData o5 = overviewManager.new OverviewData();
        o5.setTitle("活跃付费率");
        // 付费ARPU
        OverviewManager.OverviewData o6 = overviewManager.new OverviewData();
        o6.setTitle("付费ARPU" + getMoneyType());
        // 昨日新增帐号次日留存率
        OverviewManager.OverviewData o7 = overviewManager.new OverviewData();
        o7.setTitle("昨日新增帐号次留");
        OverviewManager.OverviewData o8 = overviewManager.new OverviewData();
        o8.setTitle("前日新增帐号三日留");
        OverviewManager.OverviewData o9 = overviewManager.new OverviewData();
        o9.setTitle("活跃ARPU" + getMoneyType());
//        OverviewData o10 = new OverviewData();
//        o10.setTitle("前日新增设备次留");
//        OverviewData o11 = new OverviewData();
//        o11.setTitle("前日新增设备三日留");

//        o0.setValue(activeDeviceByDayManager.getAllActiveDeviceByConditions(lookDate, queryPlatform, queryChannel, queryServer));
//        o01.setValue(newDeviceByDayManager.getAllNewDeviceByConditions(lookDate, queryPlatform, queryChannel, queryServer));
        o1.setValue(newUserByDayManager.getAllNewUserByConditionsBytag(lookDate, queryPlatform, queryChannel, queryServer, queryCountry));
//        o100.setValue(distinctNewUserByDayManager.getAllDistinctNewUserByConditions(lookDate, queryPlatform, queryChannel, queryServer));
        o2.setValue(dauByDayManager.getAllActiveUserByConditionsByTag(lookDate, queryPlatform, queryChannel, queryServer, queryCountry));
        o3.setValue(addCashUserByDayManager.getAllPayUserByConditionsByTag(lookDate, queryPlatform, queryChannel, queryServer, queryCountry));
        o4.setValue(cashAddByDayManager.getAllCashAddByConditionsByTag(lookDate, queryPlatform, queryChannel, queryServer, queryCountry));
        o7.setValue(userSecondLeftByDayManager.getAllUserSecondLeftByConditionsByTag(lookDate, queryPlatform, queryChannel, queryServer, queryCountry));
        o8.setValue(userThirdLeftByDayManager.getAllUserSecondLeftByConditionsByTag(lookDate, queryPlatform, queryChannel, queryServer, queryCountry));

//        o10.setValue(userSecondLeftByDayManager.getAllMacSecondLeftByConditions(lookDate, queryPlatform, queryChannel, queryServer));
//        o11.setValue(userThirdLeftByDayManager.getAllMacThirdLeftByConditions(lookDate, queryPlatform, queryChannel, queryServer));

        if (!"-".equals(o3.getValue()) && !"-".equals(o2.getValue()) && !"0".equals(o2.getValue())) {
            o5.setValue(PageHelper.double2Percent(Integer.parseInt(o3.getFormatValue()) * 1.0 / Integer.parseInt(o2.getFormatValue())));
        }

        if (!"-".equals(o3.getValue()) && !"-".equals(o4.getValue()) && !"0".equals(o3.getValue())) {
            o6.setValue(PageHelper.floatKeepTwoNumber((float) (Integer.parseInt(o4.getFormatValue()) * 1.0 / Integer.parseInt(o3.getFormatValue()))) + "");
        }

        if (!"-".equals(o2.getValue()) && !"-".equals(o4.getValue()) && !"0".equals(o2.getValue())) {
            o9.setValue(PageHelper.floatKeepTwoNumber((float) (Integer.parseInt(o4.getFormatValue()) * 1.0 / Integer.parseInt(o2.getFormatValue()))) + "");
        }

//        if (!"-".equals(o0.getValue()) && !"-".equals(o01.getValue()) && !"0".equals(o0.getValue())) {
//            o02.setValue(PageHelper.double2Percent((Integer.parseInt(o01.getValue()) * 1.0 / Integer.parseInt(o0.getValue()))));
//        }
    /*注销的指标bicompute里面没有计算*/
//        result.add(o0);
//        result.add(o01);
        //result.add(o02);
        result.add(o1);
        //result.add(o100);
        result.add(o2);
        //result.add(o3);
        result.add(o4);
        result.add(o5);
        result.add(o9);
        result.add(o6);
        result.add(o7);
        result.add(o8);
//        result.add(o10);
//        result.add(o11);

        return result;
    }

    // 得到今日分时和累计实时详细数据：updateType=1-分时;updateType=2-累计
    public List<OverviewDetailData> getOverviewDetailData(String updateType, Date lookDate, Date compareDate, Date lastWeekDate, String queryPlatform, String queryChannel, String queryServer, String queryCountry) {
        Map<Integer, OverviewDetailData> tmp = new LinkedHashMap<>();
        OverviewManager overviewManager = new OverviewManager();
        for (int i = 0; i < 24; i++) {
            OverviewManager.OverviewDetailData o = overviewManager.new OverviewDetailData();
            o.setHour(i);
            o.setLookDateStr(Time.getDateStr(lookDate));
            o.setCompareDateStr(Time.getDateStr(compareDate));
            o.setLastWeekDateStr(Time.getDateStr(lastWeekDate));
            tmp.put(i, o);
        }

        newUserByHourByTagManager.updateHourNewUserByConditionsByTag(updateType, tmp, lookDate, compareDate, lastWeekDate, queryPlatform, queryChannel, queryServer, queryCountry);  // 新增账号数
//        distinctNewUserByHourManager.updateHourDistinctNewUserByConditionsByTag(updateType, tmp, lookDate, compareDate, lastWeekDate, queryPlatform, queryChannel, queryServer, queryCountry);  // 去重新增账号数
        dauByHourByTagManager.updateHourActiveUserByConditionsByTag(updateType, tmp, lookDate, compareDate, lastWeekDate, queryPlatform, queryChannel, queryServer, queryCountry); // 活跃账号数
        addCashUserByHourByTagManager.updateHourPayUserByConditionsByTag(updateType, tmp, lookDate, compareDate, lastWeekDate, queryPlatform, queryChannel, queryServer, queryCountry); // 付费账号数
        cashAddByHourByTagManager.updateHourCashAddByConditionsByTag(updateType, tmp, lookDate, compareDate, lastWeekDate, queryPlatform, queryChannel, queryServer, queryCountry); // 付费金额
        newUserAddCashUserByHourByTagManager.updateHourNewUserAddCashUserByConditionsByTag(updateType, tmp, lookDate, compareDate, lastWeekDate, queryPlatform, queryChannel, queryServer, queryCountry); // 新增帐号付费人数
        newUserCashAddByHourByTagManager.updateHourNewUserCashAddByConditionsByTag(updateType, tmp, lookDate, compareDate, lastWeekDate, queryPlatform, queryChannel, queryServer, queryCountry); // 新增帐号付费金额
//        user2LeftByHourManager.updateUser2LeftHourByConditions(updateType, tmp, lookDate, compareDate, lastWeekDate, queryPlatform, queryChannel, queryServer, queryCountry);//分时累计次留

        List<OverviewDetailData> tmpList = new ArrayList<>(tmp.values());
        Collections.sort(tmpList, overviewManager.new OverviewDetailData().comparatorHour);
        return tmpList;
    }
}
